<?php

/*
 * This class is used for as Model for Product table 
 */

class Model_Country extends Model_Base {

    protected $_name = 'country';
    protected $_primaryKey = 'country_id';

    public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
    
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array("country" => "country"))
                ->where("country_id = ?", $id)
                ->where("country_status!= 2");
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    
    public function fetchEntryByREFId($id) {
        $select = $this->select()
                ->from(array("country" => "country"))
                ->where("country_ref_id = ?", $id)
                ->where("country_status!= 2");
        $data = $this->fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    /**
     *  This function is used to get Questions
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchCountry($where = "", $limit = "", $offset = "0", $order = 'country_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('con' => 'country'))
                ->where("country_status != 2")
                ->where("country_title != ''");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);

        $data = parent::fetchAll($select);
        if (!empty($data)) {
            $finalArray = array('' => '-- Select Country --');
            foreach ($data->toArray() as $_country_type) {
                $finalArray[$_country_type['country_id']] = ucfirst($_country_type['country_title']);
            }
            return $finalArray;
        } else {
            return array('' => '-- Select Country --');
        }
    }
    
    public function fetchAllCountry($where = "", $limit = "", $offset = "0", $order = 'country_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('con' => 'country'))
                ->where("country_status != 2");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);

        $data = parent::fetchAll($select);
        $finalArray = array();
        if (!empty($data)) {
            
            foreach ($data->toArray() as $_country_type) {
                $finalArray[$_country_type['country_id']] = $_country_type['country_title'];
            }
            return $finalArray;
        } else {
            return array();
        }
    }
    
    
    /**
     *  This function is used to get country
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchLISTCountry($where = "", $limit = "", $offset = "0", $order = "country_id", $sort = "ASC" , $group = "country_ref_id") {
        $select = $this->select()
                ->from(array("country" => "country"),array('*','lang_names' => new Zend_Db_Expr('GROUP_CONCAT(ln.lang_name)')))
                ->setIntegrityCheck(false)
                ->join(array('ln' => 'languages'), "country.country_lang_id = ln.lang_id",array('lang_name'))
                ->where("country_status != 2")
                ->where("country_name != ''");
        if ($where != "") {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        if($group != ""){
            $select->group($group);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    
    
     /**
     *  This function is used to get total orders
     * @return int
     */
    public function getCountryCount($where) {
        $select = $this->select()
                ->from(array("country" => "country"), Array("cnt" => "count(*)"))
                ->where("country_status != 2")
                ->where("country_name != ''");
                #->group('country_ref_id');
        if ($where != "") {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
    
    /**
     *  This function is used to delete record
     * 
     */
    public function deleteCountry($countryArray) {
        if (is_array($countryArray)) {
            foreach ($countryArray as $_country) {
                $countryData = Array();
                $countryData = $this->fetchEntryByREFId($_country);
                if (count($countryData) > 0) {
                    $data = array("country_status" => "2");
                    $this->update($data, "country_ref_id =" . (int) $_country);
                }
            }
        } else {
            $country_id = $countryArray;
            $countryData = $this->fetchEntryByREFId($country_id);
            if (count($countryData) > 0) {
                $data = array("country_status" => "2");
                $this->update($data, "country_ref_id =" . $country_id);
            }
        }
    }

}
